< previous page page_164 next page >

Page 164
Dim, Private, Static, and Public are Visual Basic keywords that define how and where the variable can be used. (You learn more about where these keywords can be used in upcoming sections.) varname and varname2 represent the names of two variables that you want to declare. As indicated in the syntax, you can specify multiple variables in the same statement as long as you separate the variables with commas. Note that the syntax shows only two variables on one line, but you can specify several. In fact, over a thousand characters can fit on one line in the Code window. From a practical standpoint, however, you should refrain from writing lines of code that are wider than the displayed Code window. This way, you make your code much easier to read because you don't have to scroll left and right when looking at it.
vartype and vartype2 represent the data types of the respective variables. A data type is a keyword that tells Visual Basic what kind of information is stored in the variable. As indicated, the variable type is an optional property. If you include the variable type, you must include the keyword As. If you do not include a variable type, the default type (usually Variant type) is used.
The following code shows the use of these declaration statements for actual variables:
Private nNumVal As Integer
Private nAvgVal As Integer, vInptVal As Variant
Static fClcAverage As Single
Dim sFirstName As String
Implicit Declaration
Declaring your variables using the Dim or other statements shown in the preceding section is best, but in many cases you can also assign a type to a variable using an implicit declaration. With this type of declaration, a special character is used at the end of the variable name when the variable is first assigned a value. The characters for each variable type are shown in Table 8.3.
Table 8.3 Special Variable Type Characters
Variable TypeCharacter
Integer%
Long&
Single!
Double#
Currency@
String$
ByteNone
BooleanNone
DateNone
ObjectNone
VariantNone

 
< previous page page_164 next page >